import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import {
	login,
	logout,
	getInfo
} from '@/api/login'

const baseUrl = config.baseUrl

const user = {
	state: {
		name: storage.get(constant.name),
		avatar: storage.get(constant.avatar),
		areaName: storage.get(constant.areaName),
		schoolId: storage.get(constant.schoolId),
		isLead: storage.get(constant.isLead)
	},

	mutations: {
		SET_NAME: (state, name) => {
			state.name = name
			storage.set(constant.name, name)
		},
		SET_AVATAR: (state, avatar) => {
			state.avatar = avatar
			storage.set(constant.avatar, avatar)
		},
		SET_AREANAME: (state, areaName) => {
			state.areaName = areaName
			storage.set(constant.areaName, areaName)
		},
		SET_SCHOOLID: (state, schoolId) => {
			state.schoolId = schoolId
			storage.set(constant.schoolId, schoolId)
		},
		SET_ISLEAD: (state, isLead) => {
			state.isLead = isLead
			storage.set(constant.isLead, isLead)
		}
	},

	actions: {
		// 登录
		Login({
			commit
		}, userInfo) {
			const loginName = userInfo.loginName.trim()
			const password = userInfo.password
			const deviceType = userInfo.deviceType
			const longitude = userInfo.latitude
			const latitude = userInfo.latitude
			return new Promise((resolve, reject) => {
				login(loginName, password, deviceType, longitude, latitude).then(res => {

					if (res.status !== 0) {
						uni.$u.toast(res.result);
						return false
					}
					resolve()
				}).catch(error => {

					reject(error)
				})
			})
		},

		// 获取用户信息
		GetInfo({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				getInfo().then(res => {
					if (res.status == 0) {
						if ('school' in res) {
							const school = res.school
							const avatar = require("@/static/images/profile.png")
							const schoolName = (school == null || school.schoolName == "" || school
								.schoolName ==
								null) ? "" : school.schoolName
							const areaName = (school == null || school.areaName == "" || school
								.areaName ==
								null) ? "" : school.areaName
							const schoolId = (school == null || school.schoolId == "" || school
								.schoolId ==
								null) ? "" : school.schoolId
							commit('SET_NAME', schoolName)
							commit('SET_AVATAR', avatar)
							commit('SET_AREANAME', areaName)
							commit('SET_SCHOOLID', schoolId)
						} else {
							const user = res.user
							const avatar = require("@/static/images/profile.png")
							const userName = (user == null || user.userName == "" || user
								.userName ==
								null) ? "" : user.userName
							const departmentName = (user == null || user.departmentName == "" ||
								user.departmentName ==
								null) ? "" : user.departmentName
							const isLead = (user == null || user.isLead == "" || user
								.isLead ==
								null) ? "" : user.isLead

							commit('SET_NAME', userName)
							commit('SET_AVATAR', avatar)
							commit('SET_AREANAME', departmentName)
							commit('SET_ISLEAD', isLead)
						}

						resolve(res)
					}

				}).catch(error => {
					reject(error)
				})
			})
		},

		// 退出系统
		LogOut({
			commit,
			state
		}) {
			return new Promise((resolve, reject) => {
				logout().then(() => {
					// 退出成功,将vuex和storage清空
					storage.clean();
					resolve()
				}).catch(error => {
					reject(error)
				})
			})
		}
	}
}

export default user